package chapter9;
import java.io.Reader;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import chapter9.po.Student;
public class MybatisTest {
    private static SqlSessionFactory sqlSessionFactory = null;
    // 初始化SqlSessionFactory对象
    static {
        try {
            // 使用MyBatis提供的Resources类加载MyBatis的配置文件
            Reader reader = Resources.getResourceAsReader("chapter9_batis.xml");
            // 构建SqlSessionFactory工厂
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    // 获取SqlSession对象的静态方法
    public static SqlSession getSession() {
        return sqlSessionFactory.openSession();
    }

    /**
     * 1.根据学号查询学生信息
     */

    public void findStudentByStunoTest() {
        // 获取SqlSession
        SqlSession sqlSession = getSession();
        // SqlSession执行映射文件中定义的SQL，并返回映射结果
        String query = "chapter9.po.mapper.StudentMapper.findStudentByStuno";
        Student stu = sqlSession.selectOne(query, 2018020222);
        // 打印输出结果
        System.out.println(stu.toString());
        // 关闭SqlSession
        sqlSession.close();
    }

    public static void main(String[] args) {
        MybatisTest test=new MybatisTest();
        test.findStudentByStunoTest();
    }

}
